﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;

namespace Tireguit.Nmon.Parser.Information
{
    public class BBBBElement:NmonInfoElement
    {
        protected new ILog _log=LogManager.GetLogger(typeof(BBBBElement));
        protected IDataParameter _numberline = null;
        protected IDataParameter _diskname = null;
        protected IDataParameter _infoline = null;
        protected IDataParameter _disktype = null;
        public string NumberLine { get; protected set; }
        public string DiskName { get; protected set; }
        public string DiskType { get; protected set; }
        public BBBBElement(IDbConnection connection)
        {
            Balise = BaliseType.BBBB;
            ListCommand = new List<System.Data.IDbCommand>();
            Connection = connection;
            Linestring = string.Empty;
            _insertreq="INSERT INTO "+NmonTables.TablesDictionnary.Instance["BBBB"]+" ([numberline],[diskname],[diskinfo],[disktype]) VALUES (@numberline,@diskname,@diskinfo,@disktype)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _numberline = Command.CreateParameter();
            _numberline.ParameterName = "@numberline";
            _numberline.DbType = DbType.String;
            _diskname = Command.CreateParameter();
            _diskname.ParameterName = "@diskname";
            _diskname.DbType = DbType.String;
            _infoline = Command.CreateParameter();
            _infoline.ParameterName = "@diskinfo";
            _infoline.DbType = DbType.String;
            _disktype = Command.CreateParameter();
            _disktype.ParameterName = "@disktype";
            _disktype.DbType = DbType.String;

        }
        public BBBBElement():this(null){}
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (Command == null)
                {
                    Command = Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);
                }
                Command.Parameters.Clear();
                _numberline.Value = NumberLine;
                _diskname.Value = DiskName;
                _infoline.Value = InfoLine;
                _disktype.Value = DiskType;
                Command.Parameters.Add(_numberline);
                Command.Parameters.Add(_diskname);
                Command.Parameters.Add(_infoline);
                Command.Parameters.Add(_disktype);

                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (elements[0] != "BBBB")
                {
                    _log.Error("Not BBBB Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                NumberLine = elements[1];
                DiskName = elements[2];
                InfoLine = elements[3];
                DiskType = elements[4];
                return true;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
